package widget;

import com.qzhy.female.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MyListView extends ListView implements OnScrollListener{

	private int startY;
	private int headViewHeight;
	private View headerView;
	private int mCurrentState=STATE_PULL_REFERSH;
	private static final int STATE_PULL_REFERSH=0;//下拉刷新
	private static final int STATE_RELEASE_REFERSH=1;//放开刷新
	private static final int STATE_REFRESHING=2;//正在刷新
	private TextView mtvTitle;
	private ImageView mImgRefresh;
	private ProgressBar mPbRefreshPb;
	private RotateAnimation rotatUp;
	private RotateAnimation rotatDown;
	private boolean IsLoadingMore=false;//判断底部是否在刷新
	public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		InitHeaderView();
		InitFootView();
	}
	public MyListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		InitHeaderView();
		InitFootView();
	}
	public MyListView(Context context) {
		super(context);
		InitHeaderView();
		InitFootView();
	}
	private void InitFootView() {
		footView = View.inflate(getContext(), R.layout.item_footview, null);
		this.addFooterView(footView);
		footView.measure(0, 0);
		footHeight = footView.getMeasuredHeight();
		footView.setPadding(0,-footHeight, 0, 0);
		this.setOnScrollListener(this);
	}
	private void InitHeaderView() {
		headerView = View.inflate(getContext(), R.layout.item_headerview, null);
		mtvTitle = (TextView) headerView.findViewById(R.id.tv_title);
		mImgRefresh = (ImageView) headerView.findViewById(R.id.img_refresh);
		mPbRefreshPb = (ProgressBar) headerView.findViewById(R.id.pb_refresh_progress);
		this.addHeaderView(headerView);
		headerView.measure(0, 0);
		headViewHeight = headerView.getMeasuredHeight();
		InitAnim();
		headerView.setPadding(0	,-headViewHeight	, 0, 0);
	}
	/**
	 * 初始化箭头的动画
	 */
	private void InitAnim() {
		//箭头朝上
		rotatUp = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f,RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		rotatUp.setDuration(200);
		rotatUp.setFillAfter(true);
		//箭头朝下
		rotatDown = new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f,RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		rotatUp.setDuration(200);
		rotatUp.setFillAfter(true);
	}
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			startY = (int) ev.getRawY();
			break;
		case MotionEvent.ACTION_MOVE:
			if(startY==-1){
				startY = (int) ev.getRawY();
			}
			int endY=(int) ev.getRawY();
			int des=endY-startY;
			if(des>0 && this.getVerticalScrollbarPosition()==0){//是上拉，并且是listview的第一个条目
				int padding=des-headViewHeight;
				headerView.setPadding(0	, padding, 0, 0);
				if(padding>=0 && mCurrentState==STATE_PULL_REFERSH){
					mCurrentState=STATE_RELEASE_REFERSH;
					refreshState();
							
				}else if(padding<0 && mCurrentState!=STATE_PULL_REFERSH){
					mCurrentState=STATE_PULL_REFERSH;
					refreshState();
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			startY=-1;
			if(mCurrentState==STATE_RELEASE_REFERSH){
				mCurrentState=STATE_REFRESHING;
				refreshState();
				headerView.setPadding(0	, 0, 0, 0);
			}else if (mCurrentState==STATE_PULL_REFERSH) {
				headerView.setPadding(0	,-headViewHeight	, 0, 0);
			}
			break;
		default:
			break;
		}
		return super.onTouchEvent(ev);
	}
	/**
	 * 刷新头部页面
	 */
	private void refreshState() {
		switch (mCurrentState) {
		case STATE_PULL_REFERSH:
			mtvTitle.setText("下拉刷新");
			mImgRefresh.setVisibility(View.VISIBLE);
			mPbRefreshPb.setVisibility(View.INVISIBLE);
			mImgRefresh.startAnimation(rotatDown);
			break;
		case STATE_RELEASE_REFERSH:
			mtvTitle.setText("松开刷新");
			mImgRefresh.setVisibility(View.VISIBLE);
			mPbRefreshPb.setVisibility(View.INVISIBLE);
			mImgRefresh.startAnimation(rotatUp);
			
			break;
		case STATE_REFRESHING:
			mtvTitle.setText("正在刷新");
			//清楚动画
			mImgRefresh.clearAnimation();
			mImgRefresh.setVisibility(View.INVISIBLE);
			mPbRefreshPb.setVisibility(View.VISIBLE);
			if(mOnfreshListener!=null){
				mOnfreshListener.refresh();
			}
			break;
		default:
			break;
		}
	}
	/**
	 * 设置上拉刷新的接口
	 */
	public void onRefreshComplete(){
		if(IsLoadingMore){//正在加载更多
			footView.setPadding(0,-footHeight, 0, 0);
			IsLoadingMore=false;
		}else {
			mCurrentState=STATE_PULL_REFERSH;
			mtvTitle.setText("上拉刷新");
			mImgRefresh.setVisibility(View.VISIBLE);
			mPbRefreshPb.setVisibility(View.INVISIBLE);
			headerView.setPadding(0	,-headViewHeight	, 0, 0);
		}
	}
	
	public onRefreshListener mOnfreshListener;
	private View footView;
	private int footHeight;
	public void setOnRefreshListener(onRefreshListener listener){
		this.mOnfreshListener=listener;
	}
	public interface onRefreshListener{
		void refresh();
		void loadMore();
	}
	
	/**
	 * 滑动监听
	 */
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if(scrollState==SCROLL_STATE_IDLE || scrollState==SCROLL_STATE_FLING){//滑动后停下来，手指松开还在滑
			if(getLastVisiblePosition()==getCount()-1 && !IsLoadingMore){
				footView.setPadding(0, 0, 0, 0);
				setSelection(getCount()-1);//改变listview的显示位置
				IsLoadingMore=true;
				//设置监听
				if(mOnfreshListener!=null){
					mOnfreshListener.loadMore();
				}
			}
		}
		
	}
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		
	}
}









 